package com.zsz.springboot.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.zsz.springboot.mapper.StudentMapper;
import com.zsz.springboot.model.Student;
import com.zsz.springboot.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
@Service(interfaceClass = StudentService.class, version = "1.0.0", timeout = 15000)

public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;


    @Override
    public Student queryStudentById(Integer id) {

        Student student = studentMapper.selectByPrimaryKey(id);
        return student;
    }

    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    @Override
    public Integer queryStudentCount() {

        //提升系统性能，用户体验提升
        //（这时为了提高系统的效率）首先去redis缓存中查找，如果有：直接使用，如果没有：去数据库查询并存放到redis缓存中
        Integer studentCount = (Integer) redisTemplate.opsForValue().get("studentCount");

        //判断redis中是否有值
        if (null == studentCount) {
            //去数据库查询
            studentCount = studentMapper.queryStudentCount();

            //并存放在redis缓存中
            redisTemplate.opsForValue().set("studentCount",studentCount,30, TimeUnit.SECONDS);
        }
        return studentCount;
    }
}
